<template>
  <BreadCrumb ref="breadcrumb" :item="item"></BreadCrumb>
  <div class="common-wrapper">
    <div class="common-content">
      <el-tabs v-model="activeName" class="demo-tabs">
        <el-tab-pane label="账号详情" name="first">
          <div class="account-infor-wrapper">
            <span>用户头像：</span>
            <div class="account-infor-content">
              <el-upload
                class="avatar-uploader"
                action="http://127.0.0.1:3000/user/uploadAvatar"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="userStore.state.image_url" :src="userStore.state.image_url" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </div>
          </div>
          <div class="account-infor-wrapper">
            <span>用户账号：</span>
            <div class="account-infor-content">
              <el-input v-model="userStore.state.account" disabled></el-input>
            </div>
          </div>
          <div class="account-infor-wrapper">
            <span>用户密码：</span>
            <div class="account-infor-content">
              <el-button type="primary" @click="openChangePassword">修改密码</el-button>
            </div>
          </div>
          <div class="account-infor-wrapper">
            <span>用户姓名：</span>
            <div class="account-infor-content">
              <el-input v-model="userStore.state.name"></el-input>
            </div>
            <div class="account-save-button">
              <el-button type="primary" @click="saveName">保存</el-button>
            </div>
          </div>
          <div class="account-infor-wrapper">
            <span>用户性别：</span>
            <div class="account-infor-content">
              <el-select v-model="userStore.state.sex">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </div>
            <div class="account-save-button">
              <el-button type="primary" @click="saveSex">保存</el-button>
            </div>
          </div>
          <div class="account-infor-wrapper">
            <span>用户身份：</span>
            <div class="account-infor-content">
              <el-input v-model="userStore.state.identity" disabled></el-input>
            </div>
          </div>
          <div class="account-infor-wrapper">
            <span>用户部门：</span>
            <div class="account-infor-content">
              <el-input v-model="userStore.state.department" disabled></el-input>
            </div>
          </div>
          <div class="account-infor-wrapper">
            <span>用户邮箱：</span>
            <div class="account-infor-content">
              <el-input v-model="userStore.state.email"></el-input>
            </div>
            <div class="account-save-button">
              <el-button type="primary" @click="saveEmail">保存</el-button>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second"></el-tab-pane>
        <el-tab-pane label="首页管理" name="third"></el-tab-pane>
        <el-tab-pane label="其他设置" name="fourth"></el-tab-pane>
      </el-tabs>
    </div>
  </div>
  <ChangePassword ref="changePasswordRef"></ChangePassword>
</template>

<script setup lang='ts'>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import { bindAccount, changeName, changeSex, changeEmail } from '@/api/userInfo'
import { useUserInfo } from '@/stores/userInfo'

import type { UploadProps } from 'element-plus'

import BreadCrumb from '@/components/BreadCrumb.vue'
import ChangePassword from './components/ChangePassword.vue'

defineOptions({
  name: ''
})

const userStore = useUserInfo()
const changePasswordRef = ref()
const breadcrumb = ref()
const item = ref({
  first: '系统设置'
})
const activeName = ref('first')

const handleAvatarSuccess: UploadProps['onSuccess'] = async (
  response,
  uploadFile
) => {
  if(response.status == 0){
    userStore.state.image_url = response.url
    ElMessage.success('更新头像成功')
    await bindAccount({
      account: userStore.state.account,
      url: response.url,
      onlyId: response.onlyId
    })
  } else {
    ElMessage.error('更新头像失败，请重新上传')
  }
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}

const openChangePassword = () => {
  changePasswordRef.value.open()
}
const id = localStorage.getItem('id')!
const saveName = async () => {
  await changeName({ id, name: userStore.state.name })
  ElMessage.success('修改成功')
}

const saveSex = async () => {
  await changeSex({ id, sex: userStore.state.sex })
  ElMessage.success('修改成功')
}

const saveEmail = async () => {
  await changeEmail({ id, email: userStore.state.email })
  ElMessage.success('修改成功')
}

</script>

<style lang="scss" scoped>
.common-wrapper {
  padding: 8px;
  background: #f5f5f5;
  height: calc(100vh - 85px);

  .common-content {
    padding: 0 10px;
    height: 100%;
    background: #fff;
  }
}

.demo-tabs>.el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>